{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Add Strings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #string #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #字符串 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: addStrings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #字符串相加"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定两个字符串形式的非负整数&nbsp;<code>num1</code> 和<code>num2</code>&nbsp;，计算它们的和并同样以字符串形式返回。</p>\n",
    "\n",
    "<p>你不能使用任何內建的用于处理大整数的库（比如 <code>BigInteger</code>），&nbsp;也不能直接将输入的字符串转换为整数形式。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num1 = \"11\", num2 = \"123\"\n",
    "<strong>输出：</strong>\"134\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num1 = \"456\", num2 = \"77\"\n",
    "<strong>输出：</strong>\"533\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num1 = \"0\", num2 = \"0\"\n",
    "<strong>输出：</strong>\"0\"\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= num1.length, num2.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>num1</code> 和<code>num2</code> 都只包含数字&nbsp;<code>0-9</code></li>\n",
    "\t<li><code>num1</code> 和<code>num2</code> 都不包含任何前导零</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [add-strings](https://leetcode.cn/problems/add-strings/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [add-strings](https://leetcode.cn/problems/add-strings/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"11\"\\n\"123\"', '\"456\"\\n\"77\"', '\"0\"\\n\"0\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        sys.set_int_max_str_digits(10**4)\n",
    "        return str(int(num1) + int(num2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        a=0\n",
    "        ans=''\n",
    "        n=max(len(num1),len(num2))\n",
    "        num1='0'*(n-len(num1))+num1\n",
    "        num2='0'*(n-len(num2))+num2\n",
    "        i=-1\n",
    "        while True:\n",
    "            k=int(num1[i])+int(num2[i])+a\n",
    "            ans=ans+str(k%10)\n",
    "            if k>=10:\n",
    "                a=1\n",
    "            else:\n",
    "                a=0\n",
    "            if i==-n:\n",
    "                break\n",
    "            i-=1\n",
    "        if a:\n",
    "            ans=ans+str(a)\n",
    "        return ans[::-1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import sys\n",
    "\n",
    "# 增加整数转换为字符串的限制\n",
    "sys.set_int_max_str_digits(10000) \n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        temp1 = temp2 = 0\n",
    "        for i in range(1,len(num1)+1):\n",
    "            temp1 += int(num1[-i]) * (10**(i - 1))\n",
    "        for i in range(1,len(num2)+1):\n",
    "            temp2 += int(num2[-i]) * (10**(i - 1))\n",
    "        return str(temp1+temp2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        n ,m = len(num1) , len(num2)\n",
    "        i ,j = n - 1,m - 1\n",
    "        f = 0\n",
    "        res = \"\"\n",
    "        while i>=0 or j >=0 :\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "\n",
    "            y = n2 + n1 + f\n",
    "            f = y // 10\n",
    "            res = str(y % 10) + res\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "\n",
    "        return \"1\" + res if f == 1 else res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        n1,n2=len(num1),len(num2)\n",
    "        if n1<n2:num1=(n2-n1)*\"0\"+num1\n",
    "        if n2<n1:num2=(n1-n2)*\"0\"+num2\n",
    "\n",
    "        num,cin=\"\",0\n",
    "        for i in range(max(n1,n2)-1,-1,-1):\n",
    "            t=int(num1[i])+int(num2[i])+cin\n",
    "            cin,m=t//10,t%10\n",
    "            num=str(m)+num\n",
    "        return num if cin==0 else \"1\"+num\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        answer = ''\n",
    "        num1 = num1[::-1]\n",
    "        num2 = num2[::-1]\n",
    "        r = 0\n",
    "        c = 0\n",
    "        while len(num1) != len(num2):\n",
    "            if len(num1)<len(num2):\n",
    "                num1 += '0'\n",
    "            else:\n",
    "                num2 += '0'\n",
    "        while r<len(num1):\n",
    "            a = int(num1[r])\n",
    "            b = int(num2[r])\n",
    "            if a+b+c <10:\n",
    "                answer += str(a+b+c)\n",
    "                c = 0\n",
    "            else:\n",
    "                answer += str(a+b+c-10)\n",
    "                c = 1\n",
    "            r += 1\n",
    "        if c == 1:\n",
    "            answer += '1'\n",
    "        return answer[::-1]\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        m, n = len(num1), len(num2)\n",
    "        i, j = m-1, n-1\n",
    "        carry = 0\n",
    "        ans = ''\n",
    "\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            s = n1 + n2 + carry\n",
    "            carry = s // 10\n",
    "            ans = str(s % 10) + ans\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        \n",
    "        return '1' + ans if carry else ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        n1 = len(num1)\n",
    "        n2 = len(num2)\n",
    "        p1 = n1-1\n",
    "        p2 = n2-1\n",
    "        res = ''\n",
    "        temp = 0\n",
    "        while p1 >= 0 or p2 >= 0:\n",
    "            # 两树的和\n",
    "            s1 = '0' if p1 < 0 else num1[p1]\n",
    "            s2 = '0' if p2 < 0 else num2[p2]\n",
    "            two_sum = int(s1)+int(s2)+temp\n",
    "            # 计算进位\n",
    "            temp = two_sum // 10\n",
    "            # 计算余数\n",
    "            left = two_sum % 10\n",
    "            res = str(left)+res\n",
    "            p1 -= 1\n",
    "            p2 -= 1\n",
    "        return '1'+res if temp == 1 else res\n",
    "\n",
    "         \n",
    "\n",
    "             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        t = 0\n",
    "        res = ''\n",
    "        l1, l2 = len(num1), len(num2)\n",
    "        x, y = l1 -1 , l2 - 1\n",
    "        while x >= 0 and y >= 0:\n",
    "            T = int(num1[x]) + int(num2[y]) + t\n",
    "            t = 1 if T >= 10 else 0\n",
    "            res = str(T % 10) + res\n",
    "            x -= 1\n",
    "            y -= 1\n",
    "        while x >= 0:\n",
    "            T = int(num1[x]) + t\n",
    "            t = 1 if T >= 10 else 0\n",
    "            res = str(T % 10) + res \n",
    "            x -= 1\n",
    "        while y >= 0:\n",
    "            T = int(num2[y]) + t\n",
    "            t = 1 if T >= 10 else 0\n",
    "            res = str(T % 10) + res \n",
    "            y -= 1\n",
    "        if t:\n",
    "            return str(t) + res\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        n1,n2 = len(num1),len(num2)\n",
    "        k1,k2,t = n1-1,n2-1,0\n",
    "        ans = ''\n",
    "        while k1>=0 or k2>=0:\n",
    "            t +=  int(num1[k1]) if k1>=0 else 0\n",
    "            t += int(num2[k2]) if k2>=0 else 0\n",
    "\n",
    "            ans = str( t % 10) + ans\n",
    "            t = t//10\n",
    "            \n",
    "            k1 -= 1\n",
    "            k2 -= 1\n",
    "\n",
    "        if t:\n",
    "            ans = str(t) + ans\n",
    "        return ans\n",
    "\n",
    "         \n",
    "\n",
    "             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = \"\"\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            tmp = n1 + n2 + carry\n",
    "            carry = tmp // 10\n",
    "            res = str(tmp % 10) + res\n",
    "            i, j = i - 1, j - 1\n",
    "        return \"1\" + res if carry else res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "\n",
    "        res = \"\"\n",
    "\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0\n",
    "\n",
    "        while i >= 0 or j >= 0:\n",
    "\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "\n",
    "            tmp = n1 + n2 + carry\n",
    "\n",
    "            carry = tmp // 10\n",
    "\n",
    "            res = str(tmp % 10) + res\n",
    "\n",
    "            i, j = i - 1, j - 1\n",
    "\n",
    "        return \"1\" + res if carry else res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i = len(num1) - 1\n",
    "        j = len(num2) - 1\n",
    "        flag = 0   # 进位标志\n",
    "        ans = \"\"\n",
    "        while (i >= 0 or j >= 0 or flag != 0):\n",
    "            x = int(num1[i]) if i >= 0 else 0\n",
    "            y = int(num2[j]) if j >= 0 else 0\n",
    "            ret = x + y + flag\n",
    "            ans += str(ret % 10)\n",
    "            flag = ret // 10\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        return ans[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        ans = \"\"\n",
    "        n, m = len(num1), len(num2)\n",
    "        i, j = n - 1, m - 1\n",
    "        carry = 0\n",
    "        while i >= 0 and j >= 0:\n",
    "            tmp = int(num1[i]) + int(num2[j]) + carry\n",
    "            carry = 1 if tmp >= 10 else 0\n",
    "            tmp %= 10\n",
    "            ans += str(tmp)\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "\n",
    "        while i >= 0:\n",
    "            tmp = int(num1[i]) + carry\n",
    "            carry = 1 if tmp >= 10 else 0\n",
    "            tmp %= 10\n",
    "            ans += str(tmp)\n",
    "            i -= 1\n",
    "        while j >= 0:\n",
    "            tmp = int(num2[j]) + carry\n",
    "            carry = 1 if tmp >= 10 else 0\n",
    "            tmp %= 10\n",
    "            ans += str(tmp)\n",
    "            j -= 1\n",
    "        if carry == 1:\n",
    "            ans += \"1\"\n",
    "        return ans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        sys.set_int_max_str_digits(10000)\n",
    "        total = int(num1) + int(num2)\n",
    "\n",
    "        return str(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = ''\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            tmp = n1 + n2 + carry\n",
    "            carry = tmp // 10\n",
    "            res = str(tmp % 10) + res\n",
    "            i, j = i - 1, j - 1\n",
    "        return '1' + res if carry else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = \"\"\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            tmp = n1 + n2 + carry\n",
    "            carry = tmp // 10\n",
    "            res = str(tmp % 10) + res\n",
    "            i, j = i - 1, j - 1\n",
    "        return \"1\" + res if carry else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = ''\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "\n",
    "            tmp = n1 + n2 + carry\n",
    "            carry = tmp // 10\n",
    "            res = str(tmp % 10) + res\n",
    "\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        return '1' + res if carry else res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = ''\n",
    "        i, j, carry = len(num1)-1, len(num2)-1, 0\n",
    "        while i>=0 or j>=0:\n",
    "            n1 = int(num1[i]) if i>=0 else 0\n",
    "            n2 = int(num2[j]) if j>=0 else 0\n",
    "            tmp_val = n1 + n2 + carry\n",
    "            carry = tmp_val//10\n",
    "            tmp_val = tmp_val%10\n",
    "            res += str(tmp_val)\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        if carry>0:\n",
    "            res += '1'\n",
    "        res = res[::-1]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i,j,p=len(num1)-1,len(num2)-1,0\n",
    "        ans=''\n",
    "        while i>=0 or j>=0:\n",
    "            n1=int(num1[i]) if i>=0 else 0\n",
    "            n2=int(num2[j]) if j>=0 else 0\n",
    "            s=n1+n2+p\n",
    "            p=s//10\n",
    "            ans=str(s%10)+ans\n",
    "            i,j=i-1,j-1\n",
    "        return '1'+ans if p else ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = \"\"\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            tmp = n1 + n2 + carry\n",
    "            carry = tmp // 10\n",
    "            res = str(tmp % 10) + res\n",
    "            i, j = i - 1, j - 1\n",
    "        return \"1\" + res if carry else res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        sys.set_int_max_str_digits(10**5)\n",
    "        return str(int(num1)+int(num2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = \"\"\n",
    "        len1 = len(num1)\n",
    "        len2 = len(num2)\n",
    "        index1 = len1 - 1\n",
    "        index2 = len2 - 1\n",
    "        count = 0\n",
    "        while index1>=0 or index2>=0:\n",
    "            n1 = int(num1[index1]) if index1 >= 0 else 0\n",
    "            n2 = int(num2[index2]) if index2 >= 0 else 0\n",
    "            s = n1 + n2 + count\n",
    "            val = str(s % 10)\n",
    "            count = s//10\n",
    "            res = val + res\n",
    "            index1 -= 1\n",
    "            index2 -= 1\n",
    "        return \"1\" + res if count else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i = len(num1) - 1\n",
    "        j = len(num2) - 1\n",
    "        carry = 0\n",
    "        new_num = \"\"\n",
    "        while i >= 0 or j >= 0 or carry >= 1:\n",
    "            n1 = 0 if i < 0 else int(num1[i])\n",
    "            n2 = 0 if j < 0 else int(num2[j])\n",
    "            s = n1 + n2 + carry\n",
    "            carry, n = divmod(s, 10)\n",
    "            new_num = str(n) + new_num\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        return new_num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        num1 = (len(num2)-len(num1))*'0' + num1\n",
    "        num2 = (len(num1)-len(num2))*'0' + num2\n",
    "        lens = len(num1)\n",
    "        num1 = num1[::-1]\n",
    "        num2 = num2[::-1]\n",
    "        shang = 0\n",
    "        str1 = ''\n",
    "        for i in range(lens):\n",
    "            sum1 = int(num1[i]) + int(num2[i]) + shang\n",
    "            yvshu = sum1 % 10\n",
    "            shang = sum1 // 10\n",
    "            if i != lens -1 :\n",
    "                str1 = str(yvshu) + str1\n",
    "            else:\n",
    "                str1 = str(sum1) + str1\n",
    "        return str1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = ''\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            tmp = n1 + n2 + carry\n",
    "            carry = tmp // 10\n",
    "            res = str(tmp % 10) + res\n",
    "            i, j = i - 1, j - 1\n",
    "        return '1' + res if carry else res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i = len(num1)-1\n",
    "        j = len(num2)-1\n",
    "        res = \"\"\n",
    "        carry = 0\n",
    "        while i >=0 or j >= 0:\n",
    "            n1 = num1[i] if i >= 0 else '0'\n",
    "            n2 = num2[j] if j >= 0 else '0'\n",
    "            temp = ord(n1) + ord(n2) - 2*ord('0') + carry\n",
    "            cur = temp%10 \n",
    "            carry = temp//10\n",
    "            res = chr(cur+48) + res\n",
    "            i -= 1\n",
    "            j -= 1 \n",
    "        return '1' + res if carry != 0 else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = \"\"\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            tmp = n1 + n2 + carry\n",
    "            carry = tmp // 10\n",
    "            res = str(tmp % 10) + res\n",
    "            i, j = i - 1, j - 1\n",
    "        return \"1\" + res if carry else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        n1 = len(num1)\n",
    "        n2 = len(num2)\n",
    "        i = n1 - 1\n",
    "        j = n2 - 1\n",
    "        res = []\n",
    "        carry = 0\n",
    "        while i >= 0 or j >= 0 or carry:\n",
    "            d1 = int(num1[i]) if i >= 0 else 0\n",
    "            d2 = int(num2[j]) if j >= 0 else 0\n",
    "            tmp = d1 + d2 + carry\n",
    "            res.append('%d'%(tmp % 10))\n",
    "            carry = tmp // 10\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        return \"\".join(res[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        n1 = len(num1)\n",
    "        n2 = len(num2)\n",
    "        i, j = n1-1, n2-1\n",
    "        ans = \"\"\n",
    "        to_add = 0\n",
    "        while i >= 0 or j >=0:\n",
    "\n",
    "            n1 = int(num1[i]) if i >=0 else 0\n",
    "            n2 = int(num2[j]) if j >=0 else 0\n",
    "            temp = n1 + n2 + to_add\n",
    "            to_add = temp // 10\n",
    "            ans = str(temp % 10) + ans\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "\n",
    "        return ans if to_add == 0 else \"1\" + ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        p1 = len(num1) - 1\n",
    "        p2 = len(num2) - 1\n",
    "        res = \"\"\n",
    "        carry = 0\n",
    "\n",
    "        while p1 >= 0 and p2 >= 0:\n",
    "            n1 = int(num1[p1])\n",
    "            n2 = int(num2[p2])\n",
    "            temp = n1 + n2 + carry\n",
    "            carry = temp // 10\n",
    "            res = str(temp % 10) + res\n",
    "            p1 -= 1\n",
    "            p2 -= 1\n",
    "\n",
    "        if p1 >= 0:\n",
    "            for i in range(p1, -1, -1):\n",
    "                temp = int(num1[i]) + carry\n",
    "                carry = temp // 10\n",
    "                res = str(temp % 10) + res\n",
    "        if p2 >= 0:\n",
    "            for i in range(p2, -1, -1):\n",
    "                temp = int(num2[i]) + carry\n",
    "                carry = temp // 10\n",
    "                res = str(temp % 10) + res\n",
    "\n",
    "        if carry != 0:\n",
    "            res = '1' + res\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "\n",
    "        res = \"\"\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0\n",
    "\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            tmp = n1 + n2 + carry\n",
    "            carry = tmp // 10\n",
    "            res = str(tmp % 10) + res\n",
    "            i, j = i - 1, j - 1\n",
    "\n",
    "        return \"1\" + res if carry else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        sys.set_int_max_str_digits(50000)\n",
    "        return str(int(num1) +int(num2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i, j = len(num1) - 1, len(num2) - 1\n",
    "        add = 0\n",
    "        res = \"\"\n",
    "        while i >= 0 or j >= 0:\n",
    "            x = int(num1[i]) if i >= 0 else 0\n",
    "            y = int(num2[j]) if j >= 0 else 0\n",
    "            result = x + y + add\n",
    "            res = str(result % 10) + res\n",
    "            add = result // 10\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        return \"1\" + res if add else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        plus = 0\n",
    "        res = ''\n",
    "        i = len(num1) - 1\n",
    "        j = len(num2) - 1\n",
    "        while i >= 0 or j >= 0 or plus > 0:\n",
    "            x = 0\n",
    "            if i >= 0:\n",
    "                x = int(num1[i])\n",
    "            y = 0\n",
    "            if j >= 0:\n",
    "                y = int(num2[j])\n",
    "            sum = x + y + plus\n",
    "            if sum >= 10:\n",
    "                sum = sum % 10\n",
    "                plus = 1\n",
    "            else:\n",
    "                plus = 0\n",
    "            res = str(sum) + res\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i,j=len(num1)-1,len(num2)-1\n",
    "        res=\"\"\n",
    "        carry=0\n",
    "        while i>=0 or j>=0:\n",
    "            n1=int(num1[i]) if i>=0 else 0\n",
    "            n2=int(num2[j]) if j>=0 else 0\n",
    "            temp=n1 + n2 +carry\n",
    "            carry = temp //10\n",
    "            res = str(temp%10) + res\n",
    "            i,j = i-1, j-1\n",
    "        return \"1\"+res if carry else res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        ans = ''\n",
    "        i,j, carry = len(num1)-1, len(num2)-1 ,0\n",
    "\n",
    "        while i>=0 or j>=0:\n",
    "            n1 = int(num1[i]) if i>=0 else 0\n",
    "            n2 = int(num2[j]) if j>=0 else 0\n",
    "\n",
    "            temp = n1 + n2 + carry\n",
    "            carry = temp // 10\n",
    "            ans = str(temp%10) + ans\n",
    "            i ,j = i-1 , j-1\n",
    "\n",
    "        if carry !=0:\n",
    "            return '1'+ans\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, n1: str, n2: str) -> str:\n",
    "        s=''\n",
    "        a=max(len(n1),len(n2))\n",
    "        n1,n2,t=n1.rjust(a,'0'),n2.rjust(a,'0'),0\n",
    "        n1,n2=list(n1),list(n2)\n",
    "        for i in range(a-1,-1,-1):\n",
    "            m=eval(n1[i])+eval(n2[i])+t\n",
    "            if m>9:\n",
    "                s=str(m-10)+s\n",
    "                t=1\n",
    "            else:\n",
    "                s=str(m)+s\n",
    "                t=0\n",
    "        if t==1:\n",
    "            s='1'+s\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        n1 = len(num1)-1\n",
    "        n2 = len(num2)-1\n",
    "        carry = 0\n",
    "        res = ''\n",
    "        while n1>=0 or n2 >=0:\n",
    "            a = '0' if n1<0 else num1[n1]\n",
    "            b = '0' if n2<0 else num2[n2]\n",
    "            tmp = int(a)+int(b)+carry\n",
    "            carry = tmp//10\n",
    "            res = str(tmp%10) + res\n",
    "            n1-=1\n",
    "            n2-=1\n",
    "        \n",
    "        if carry>0:\n",
    "            res = '1' + res\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        # 1、倒序看 \n",
    "        # 2、用divmod对10求余数和商，最后再考虑进位\n",
    "\n",
    "        # res = ''\n",
    "        # count = 0\n",
    "        # i = len(num1) - 1\n",
    "        # j = len(num2) - 1\n",
    "        # while i >= 0 or j >= 0:\n",
    "        #     tmp1 = int(num1[i]) if i >= 0 else 0\n",
    "        #     tmp2 = int(num2[j]) if j >= 0 else 0\n",
    "        #     count, tmp = divmod(tmp1+tmp2+count, 10)\n",
    "        #     res = str(tmp) + res\n",
    "        #     i -= 1\n",
    "        #     j -= 1\n",
    "        # return '1' + res if count else res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        res = '' \n",
    "        count = 0\n",
    "        i = len(num1) - 1 \n",
    "        j = len(num2) - 1\n",
    "        while i >= 0 or j >= 0:\n",
    "            tmp1 = int(num1[i]) if i >= 0 else 0 \n",
    "            tmp2 = int(num2[j]) if j >= 0 else 0 \n",
    "            count, tmp = divmod(tmp1+tmp2+count, 10) \n",
    "            res = str(tmp) + res \n",
    "            i -= 1\n",
    "            j -= 1 \n",
    "        return '1' + res if count else res \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        if not num1 and not num2:\n",
    "            return ''\n",
    "        if not num1:\n",
    "            return num2\n",
    "        if not num2:\n",
    "            return num1\n",
    "        num1 = num1[::-1]\n",
    "        num2 = num2[::-1]\n",
    "        i = 0\n",
    "        j = 0\n",
    "        res = ''\n",
    "        int_part = 0\n",
    "        while i < len(num1) or j < len(num2):\n",
    "            while i < len(num1) and j < len(num2):\n",
    "                s = int(num1[i]) + int(num2[j]) + int_part\n",
    "                int_part = s // 10\n",
    "                if s >= 10:\n",
    "                    res += str(s % 10)\n",
    "                else:\n",
    "                    res += str(s)\n",
    "                i += 1\n",
    "                j += 1\n",
    "            while i < len(num1):\n",
    "                s1 = int(num1[i]) + int_part\n",
    "                int_part = s1 // 10\n",
    "                if s1 >= 10:\n",
    "                    res += str(s1 % 10)\n",
    "                else:\n",
    "                    res += str(s1)\n",
    "                i += 1\n",
    "            while j < len(num2):\n",
    "                s2 = int(num2[j]) + int_part\n",
    "                int_part = s2 // 10\n",
    "                if s2 >= 10:\n",
    "                    res += str(s2 % 10)\n",
    "                else:\n",
    "                    res += str(s2)\n",
    "                j += 1\n",
    "        if int_part != 0:\n",
    "            res += str(int_part)\n",
    "        return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = \"\"\n",
    "        i, j = len(num1) - 1, len(num2) - 1\n",
    "        carry, sum = 0, 0\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            sum = n1 + n2 + carry\n",
    "            carry = sum // 10\n",
    "            res = str(sum % 10) + res\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        return(\"1\" + res if carry else res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        l1, l2 = len(num1), len(num2)\n",
    "        if l1 < l2:\n",
    "            num1 = '0' * (l2 - l1) + num1\n",
    "        else:\n",
    "            num2 = '0' * (l1 - l2) + num2\n",
    "        jinwei, ans = 0, \"\"\n",
    "        for i in range(max(l1, l2) - 1, -1, -1):\n",
    "            tmp = ord(num1[i]) + ord(num2[i]) - 96 + jinwei\n",
    "            if tmp >= 10:\n",
    "                ans = str(tmp - 10) + ans\n",
    "                jinwei = 1\n",
    "            else:\n",
    "                ans = str(tmp) + ans\n",
    "                jinwei = 0\n",
    "        if jinwei == 1:\n",
    "            ans = '1' + ans\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        num1 = num1[::-1]\n",
    "        num2 = num2[::-1]\n",
    "        i = 0\n",
    "        len_1, len_2 = len(num1), len(num2)\n",
    "        jinwei = 0\n",
    "        result = \"\"\n",
    "        while i < len_1 and i < len_2:\n",
    "            temp = jinwei + int(num1[i]) + int(num2[i])\n",
    "            jinwei = temp // 10\n",
    "            result = str(temp % 10) + result\n",
    "            i += 1\n",
    "        \n",
    "        temp_ = num1 if len_1 > len_2 else num2\n",
    "        for j in range(i, len(temp_)):\n",
    "            temp = jinwei + int(temp_[j])\n",
    "            jinwei = temp // 10\n",
    "            result = str(temp % 10) + result\n",
    "        \n",
    "        if jinwei:\n",
    "            result = '1' + result\n",
    "        return result\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        p1 = len(num1) - 1\n",
    "        p2 = len(num2) - 1\n",
    "        res = \"\"\n",
    "        carry = 0\n",
    "\n",
    "        while p1 >= 0 and p2 >= 0:\n",
    "            n1 = int(num1[p1])\n",
    "            n2 = int(num2[p2])\n",
    "            temp = n1 + n2 + carry\n",
    "            carry = temp // 10\n",
    "            res = str(temp % 10) + res\n",
    "            p1 -= 1\n",
    "            p2 -= 1\n",
    "\n",
    "        if p1 >= 0:\n",
    "            for i in range(p1, -1, -1):\n",
    "                temp = int(num1[i]) + carry\n",
    "                carry = temp // 10\n",
    "                res = str(temp % 10) + res\n",
    "        if p2 >= 0:\n",
    "            for i in range(p2, -1, -1):\n",
    "                temp = int(num2[i]) + carry\n",
    "                carry = temp // 10\n",
    "                res = str(temp % 10) + res\n",
    "\n",
    "        if carry != 0:\n",
    "            res = str(carry) + res\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = \"\"\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            tmp = n1 + n2 + carry\n",
    "            carry = tmp // 10\n",
    "            res = str(tmp % 10) + res\n",
    "            i, j = i - 1, j - 1\n",
    "        return \"1\" + res if carry else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = ''\n",
    "        i, j, carry = len(num1)-1, len(num2)-1, 0\n",
    "        while i>=0 or j>=0:\n",
    "            n1 = int(num1[i]) if i>=0 else 0\n",
    "            n2 = int(num2[j]) if j>=0 else 0\n",
    "            tmp = n1 + n2 + carry\n",
    "            carry = tmp // 10\n",
    "            res = str(tmp % 10) + res\n",
    "            i, j = i - 1, j - 1\n",
    "        return '1' + res if carry else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = \"\"\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0\n",
    "        while i >= 0 or j >= 0:\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            tmp = n1 + n2 + carry\n",
    "            carry = tmp // 10\n",
    "            res = str(tmp % 10) + res\n",
    "            i, j = i - 1, j - 1\n",
    "        \n",
    "        return \"1\" + res if carry else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res=''\n",
    "\n",
    "        i,j=len(num1)-1,len(num2)-1\n",
    "        carry=0\n",
    "\n",
    "        while i>=0 or j>=0:\n",
    "            x = int(num1[i]) if i >= 0 else 0\n",
    "            y = int(num2[j]) if j >= 0 else 0\n",
    "\n",
    "            tmp=x+y+carry\n",
    "\n",
    "            carry=tmp//10\n",
    "            dig=tmp%10\n",
    "\n",
    "            res=str(dig)+res\n",
    "\n",
    "            i, j = i - 1, j - 1\n",
    "\n",
    "        # return \"1\" + res if carry else res\n",
    "        if carry==0:return res\n",
    "        else:return '1'+res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i, j = len(num1) - 1, len(num2) - 1\n",
    "        ans = \"\"\n",
    "        flag = 0\n",
    "        while(i >= 0 or j >= 0 or flag):\n",
    "            n1 = int(num1[i]) if i >= 0 else 0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0\n",
    "            sum = n1 + n2\n",
    "            \n",
    "            cur = (sum % 10 + flag) % 10\n",
    "            ans += str(cur)\n",
    "            print (cur)\n",
    "\n",
    "            flag = (sum + flag) // 10\n",
    "            print(sum, flag)\n",
    "            i-=1\n",
    "            j-=1\n",
    "        \n",
    "        return ans[::-1] if not flag else '1' + ans[::-1] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        # num1 位数\n",
    "        digit1 = len(num1) - 1\n",
    "        # num2 位数\n",
    "        digit2 = len(num2) - 1\n",
    "\n",
    "        # 进位\n",
    "        carry = 0\n",
    "        # sum 存储反向结果\n",
    "        sum = []\n",
    "        # 逆序相加\n",
    "        while carry > 0 or digit1 >= 0 or digit2 >= 0:\n",
    "            # 获取对应位数上的数字\n",
    "            num1_d = int(num1[digit1]) if digit1 >= 0 else 0\n",
    "            num2_d = int(num2[digit2]) if digit2 >= 0 else 0\n",
    "            digit1 -= 1\n",
    "            digit2 -= 1\n",
    "            # 计算结果，存储，进位\n",
    "            num = num1_d+num2_d+carry\n",
    "            sum.append('%d'%(num%10))\n",
    "            carry = num // 10\n",
    "        # 返回计算结果\n",
    "        return \"\".join(sum[::-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        carry = 0\n",
    "        p1, p2 = len(num1) - 1, len(num2) - 1\n",
    "        strs = []\n",
    "        while carry or p1 >= 0 or p2 >= 0:\n",
    "            v = carry\n",
    "            if p1 >= 0:\n",
    "                v += int(num1[p1])\n",
    "                p1 -= 1\n",
    "            if p2 >= 0:\n",
    "                v += int(num2[p2])\n",
    "                p2 -= 1\n",
    "\n",
    "            carry, v = divmod(v, 10)\n",
    "            strs.append(str(v))\n",
    "        return ''.join(reversed(strs))\n",
    "\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i,j = len(num1)-1,len(num2)-1\n",
    "        ans = []\n",
    "        c = 0\n",
    "        while i >= 0 or j >= 0 or c:\n",
    "            a = 0 if i < 0 else int(num1[i])\n",
    "            b = 0 if j < 0 else int(num2[j])\n",
    "            c,v = divmod(a +b+c,10)\n",
    "            ans.append(str(v))\n",
    "            i,j = i-1,j-1\n",
    "        return \"\".join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        m, n = len(num1), len(num2)\n",
    "        reminder = 0\n",
    "        i, j = m - 1, n - 1\n",
    "        char_list = []\n",
    "        while i >= 0 or j >= 0 or reminder:\n",
    "            if i >= 0:\n",
    "                reminder += int(num1[i])\n",
    "                i -= 1\n",
    "            if j >= 0:\n",
    "                reminder += int(num2[j])\n",
    "                j -= 1\n",
    "            char_list.append(str(reminder % 10))\n",
    "            reminder = reminder // 10\n",
    "        return ''.join(char_list[::-1])\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        index_1 = len(num1) - 1\n",
    "        index_2 = len(num2) - 1\n",
    "        carry = 0\n",
    "        res = []\n",
    "        while index_1 >= 0 or index_2 >= 0 or carry > 0:\n",
    "            num1_d = int(num1[index_1]) if index_1 >= 0 else 0\n",
    "            num2_d = int(num2[index_2]) if index_2 >= 0 else 0\n",
    "            index_1 -= 1\n",
    "            index_2 -= 1\n",
    "            num = num1_d + num2_d + carry\n",
    "            res.append(str(num % 10))\n",
    "            carry = num//10\n",
    "        return \"\".join(res[::-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "   \n",
    "\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i = len(num1) - 1\n",
    "        j = len(num2) - 1\n",
    "        tempt = 0\n",
    "        ans = \"\"\n",
    "        counnt = 1\n",
    "        while(i >= 0 and j >= 0 ):\n",
    "            n1 = int(num1[i])\n",
    "            n2 = int(num2[j])\n",
    "            rete = (n1 + n2 + tempt) % 10\n",
    "            tempt = (n1 + n2 + tempt) // 10\n",
    "           \n",
    "            ans = str(rete) + ans \n",
    "            counnt = counnt * 10\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        if i == -1 :\n",
    "            while (j >= 0 ):\n",
    "                n2 = int(num2[j])\n",
    "                rete = (n2 + tempt) % 10\n",
    "                tempt = (n2 + tempt) // 10\n",
    "                ans = str(rete) + ans\n",
    "                counnt = counnt * 10\n",
    "                j -= 1\n",
    "        elif j == -1:\n",
    "            while (i >= 0 ):\n",
    "                n1 = int(num1[i])\n",
    "                rete = (n1 + tempt) % 10\n",
    "                tempt = (n1 + tempt) // 10\n",
    "                ans = str(rete) + ans\n",
    "                counnt = counnt * 10\n",
    "                i -= 1\n",
    "        if(tempt == 1):\n",
    "            return  \"1\"+ans\n",
    "        else:\n",
    "            return ans\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        len1, len2 = len(num1), len(num2);\n",
    "        l = max(len1, len2)\n",
    "        num1, num2, num = num1.zfill(l)[::-1], num2.zfill(l)[::-1], [\"0\"]\n",
    "        for i in range(l):\n",
    "            if len(num[-1]) == 2:\n",
    "                num[-1] = num[-1][1]\n",
    "                num.append(str(int(num1[i]) + int(num2[i]) + 1))\n",
    "            else:\n",
    "                num.append(str(int(num1[i]) + int(num2[i])))\n",
    "        return ''.join(num[:0:-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        # 让 num1 总是更长的一个\n",
    "        if len(num2) > len(num1):\n",
    "            num1, num2 = num2, num1\n",
    "\n",
    "        # 将 num2 用 0 填充，使其与 num1 的长度相同\n",
    "        num2 = num2.zfill(len(num1))\n",
    "\n",
    "        carry = 0  # 进位\n",
    "        result = []\n",
    "\n",
    "        for i in range(len(num1) - 1, -1, -1):\n",
    "            temp_sum = int(num1[i]) + int(num2[i]) + carry\n",
    "            result.append(str(temp_sum % 10))\n",
    "            carry = temp_sum // 10\n",
    "\n",
    "        if carry:  # 如果还有剩余的进位\n",
    "            result.append(str(carry))\n",
    "\n",
    "        return ''.join(result[::-1])  # 反转字符串并合并\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = []\n",
    "        p1, p2 = len(num1) - 1, len(num2) - 1\n",
    "        carry = 0\n",
    "\n",
    "        while p1 >= 0 and p2 >= 0:\n",
    "            cur = carry + int(num1[p1]) + int(num2[p2])\n",
    "            carry = cur // 10\n",
    "            cur = cur % 10\n",
    "            res.append(str(cur))\n",
    "            p1 -= 1\n",
    "            p2 -= 1\n",
    "        \n",
    "        while p1 >= 0:\n",
    "            cur = carry + int(num1[p1])\n",
    "            carry = cur // 10\n",
    "            cur = cur % 10\n",
    "            res.append(str(cur))\n",
    "            p1 -= 1\n",
    "        \n",
    "        while p2 >= 0:\n",
    "            cur = carry + int(num2[p2])\n",
    "            carry = cur // 10\n",
    "            cur = cur % 10\n",
    "            res.append(str(cur))\n",
    "            p2 -= 1\n",
    "        if carry:\n",
    "            res.append(str(carry))\n",
    "        return ''.join(res[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\r\n",
    "        result=''\r\n",
    "        up=0\r\n",
    "        i,j=len(num1)-1,len(num2)-1\r\n",
    "        while i >= 0 and j >= 0:\r\n",
    "            a,b=int(num1[i]),int(num2[j])\r\n",
    "            c=a+b+up\r\n",
    "            result=str(c%10)+result\r\n",
    "            up=c//10\r\n",
    "            i-=1\r\n",
    "            j-=1\r\n",
    "        while i >= 0:\r\n",
    "            a=int(num1[i])\r\n",
    "            c=a+up\r\n",
    "            result=str(c%10)+result\r\n",
    "            up=c//10\r\n",
    "            i-=1\r\n",
    "        while j >= 0:\r\n",
    "            b=int(num2[j])\r\n",
    "            c=b+up\r\n",
    "            result=str(c%10)+result\r\n",
    "            up=c//10\r\n",
    "            j-=1\r\n",
    "        return '1'+result if up == 1 else result\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        idx = 0\n",
    "        curr_logit = 0\n",
    "        next_logit = 0\n",
    "        out = []\n",
    "        while 1:\n",
    "            idx1 = len(num1)-1-idx\n",
    "            idx2 = len(num2)-1-idx\n",
    "            n1 = 0\n",
    "            n2 = 0\n",
    "            if idx1 <0 and idx2<0:break\n",
    "            if idx1>=0:\n",
    "                n1 = int(num1[idx1])\n",
    "            if idx2>=0:\n",
    "                n2 = int(num2[idx2])\n",
    "            total = next_logit + n1 + n2\n",
    "            curr_logit = total%10\n",
    "            next_logit = int((total - curr_logit)/10)\n",
    "            out.append(str(curr_logit))\n",
    "            idx +=1\n",
    "        if next_logit>0:\n",
    "            out.append(str(next_logit))\n",
    "        return ''.join(out[::-1])\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        add = 0\n",
    "        ans = []\n",
    "        m, n = len(num1)-1, len(num2)-1\n",
    "        while m>-1 or n>-1 or add>0:\n",
    "            x = int(num1[m]) if m>=0 else 0\n",
    "            y = int(num2[n]) if n>=0 else 0\n",
    "            val = x+y+add\n",
    "            ans.append(str(val%10))\n",
    "            add = val//10\n",
    "            m-=1\n",
    "            n-=1\n",
    "        if add>0:\n",
    "            ans.append(str(add))\n",
    "        return ''.join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        if len(num1) > len(num2):\n",
    "            return self.addStrings(num2,num1)\n",
    "        \n",
    "        nums1 = [int(i) for i in num1]\n",
    "        nums2 = [int(i) for i in num2]\n",
    "        tmp = [0] * (len(nums2) - len(num1))\n",
    "        nums1 = tmp + nums1\n",
    "        addition = 0\n",
    "        ans = []\n",
    "        for i in range(len(nums2)-1, -1 ,-1):\n",
    "            # print(f'nums1 : {nums1[i]} + nums2 : {nums2[i]}')\n",
    "            if nums1[i] + nums2[i] + addition >= 10:\n",
    "                \n",
    "                ans.append(str(nums1[i] + nums2[i] + addition - 10))\n",
    "                addition = 1\n",
    "            else :\n",
    "                ans.append(str(nums1[i] + nums2[i] + addition))\n",
    "                addition = 0\n",
    "        if addition == 1:\n",
    "            ans.append(str(1))\n",
    "        # print(ans)\n",
    "        s = \"\".join(ans[::-1])\n",
    "        \n",
    "\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        add = 0\n",
    "        ans = []\n",
    "        m, n = len(num1)-1, len(num2)-1\n",
    "        while m>-1 or n>-1 or add>0:\n",
    "            x = int(num1[m]) if m>=0 else 0\n",
    "            y = int(num2[n]) if n>=0 else 0\n",
    "            val = x+y+add\n",
    "            ans.append(str(val%10))\n",
    "            add = val//10\n",
    "            m-=1\n",
    "            n-=1\n",
    "        return ''.join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        res = []  # 初始化结果列表\n",
    "        i, j, carry = len(num1) - 1, len(num2) - 1, 0  # i和j分别指向num1和num2的末尾，carry表示进位\n",
    "        while i >= 0 or j >= 0 or carry:  # 只要还有未处理的数字或者进位不为0\n",
    "            n1 = int(num1[i]) if i >= 0 else 0  # 如果i>=0，取num1的第i个字符并转换为整数，否则取0\n",
    "            n2 = int(num2[j]) if j >= 0 else 0  # 如果j>=0，取num2的第j个字符并转换为整数，否则取0\n",
    "            carry, curr = divmod(n1 + n2 + carry, 10)  # 计算和，并得到新的进位和当前位的值\n",
    "            res.append(str(curr))  # 将当前位的值转换为字符串并添加到结果中\n",
    "            i, j = i - 1, j - 1  # 移动i和j\n",
    "        return ''.join(res[::-1])  # 反转结果并将其转换为字符串\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i = len(num1) -1\n",
    "        j = len(num2) -1\n",
    "        c = 0\n",
    "        new_num = ['0']*max(len(num1)+1,len(num2)+1)\n",
    "        index = -1\n",
    "        # print('new_num:',new_num)\n",
    "        while i >= 0 or j >= 0:\n",
    "            value = 0\n",
    "            # print(\"index ={},num1[i]={},num2[j]={},c = {}\".format(index,num1[i],num2[j],c))\n",
    "            if i >= 0:\n",
    "                value = value+int(num1[i])\n",
    "            if j >= 0:\n",
    "                value = value +int(num2[j])\n",
    "            value += c\n",
    "\n",
    "            if value >= 10:\n",
    "                value = value%10\n",
    "                c = 1\n",
    "            else:\n",
    "                c = 0\n",
    "            # print('index:',index)\n",
    "            new_num[index] = str(value)\n",
    "            index -= 1\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "            # print('i,j=',i,j)\n",
    "        #\n",
    "            # print('after:',new_num)\n",
    "        if c != 0:\n",
    "            new_num[index] = str(c)\n",
    "        if new_num[0] =='0':\n",
    "            return \"\".join(new_num[1:])\n",
    "        else:\n",
    "            return \"\".join(new_num)\n",
    "            \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i,j = len(num1)-1, len(num2)-1\n",
    "        ans = []\n",
    "        c = 0\n",
    "        while i>=0 or j>=0 or c:\n",
    "            a = 0 if i <0 else int(num1[i])\n",
    "            b = 0 if j <0 else int(num2[j])\n",
    "            v = (a+b+c)%10\n",
    "            c = (a+b+c)//10\n",
    "            \n",
    "            ans.append(str(v))\n",
    "            i -=1\n",
    "            j -=1\n",
    "        return \"\".join(ans[::-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        if len(num1) > len(num2):\n",
    "            min_num = f'{\"0\"*(len(num1)-len(num2))}{num2}'\n",
    "            max_num = num1\n",
    "        else:\n",
    "            min_num = f'{\"0\"*(len(num2)-len(num1))}{num1}'\n",
    "            max_num = num2\n",
    "        i = len(max_num) - 1\n",
    "        result = []\n",
    "        plus = 0\n",
    "        while i > -1:\n",
    "            tmp = int(max_num[i]) + int(min_num[i]) + plus\n",
    "            value = str(tmp) if tmp < 10 else str(tmp % 10)\n",
    "            result.insert(0, value)\n",
    "            plus = 0 if tmp < 10 else 1\n",
    "            i -= 1\n",
    "        if plus:\n",
    "            result.insert(0, '1')\n",
    "        return ''.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i = len(num1) - 1\n",
    "        j = len(num2) - 1\n",
    "        carry = 0\n",
    "        res = []\n",
    "        while i >= 0 or j >= 0:\n",
    "            a = '0' if i<0 else num1[i]\n",
    "            b = '0' if j<0 else num2[j]\n",
    "            tmp = int(a) + int(b) + carry\n",
    "            carry, tmp = tmp//10, tmp%10\n",
    "            res.append(str(tmp))\n",
    "            i-=1\n",
    "            j-=1\n",
    "        \n",
    "        if carry:\n",
    "            res.append('1')\n",
    "        \n",
    "        return \"\".join(res[::-1])\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        m, n = len(num1) - 1, len(num2) - 1\n",
    "        step = 0\n",
    "        s = []\n",
    "        while m >= 0 or n >= 0 or step > 0:\n",
    "            a = int(num1[m]) if m >= 0 else 0\n",
    "            b = int(num2[n]) if n >= 0 else 0\n",
    "            cur = a + b + step\n",
    "            s.append(str(cur % 10))\n",
    "            step = cur // 10\n",
    "            m -=1\n",
    "            n -=1\n",
    "\n",
    "        return \"\".join(reversed(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "\n",
    "        n, m = len(num1)-1, len(num2)-1\n",
    "        ans = []\n",
    "        c = 0\n",
    "        while n>-1 or m>-1 or c:\n",
    "            a = 0 if n<0 else int(num1[n])\n",
    "            b = 0 if m<0 else int(num2[m])\n",
    "            c, v = divmod(a+b+c, 10)\n",
    "            ans.append(str(v))\n",
    "            n, m = n-1, m-1\n",
    "        return ''.join(ans[::-1])\n",
    "\n",
    "# class Solution:\n",
    "#     def addStrings(self, num1: str, num2: str) -> str:\n",
    "#         i, j = len(num1) - 1, len(num2) - 1\n",
    "#         ans = []\n",
    "#         c = 0\n",
    "#         while i >= 0 or j >= 0 or c:\n",
    "#             a = 0 if i < 0 else int(num1[i])\n",
    "#             b = 0 if j < 0 else int(num2[j])\n",
    "#             c, v = divmod(a + b + c, 10)\n",
    "#             ans.append(str(v))\n",
    "#             i, j = i - 1, j - 1\n",
    "#         return \"\".join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        # 反转两个字符串，方便从低位到高位相加\n",
    "        num1 = num1[::-1]\n",
    "        num2 = num2[::-1]\n",
    "\n",
    "        res = [] # 初始化结果字符串\n",
    "        carry = 0 # 初始化进位\n",
    "\n",
    "        # 逐位相加\n",
    "        i= 0\n",
    "        while i <len(num1) or i<len(num2):\n",
    "            # 获取当前位的数字，如果已经超过字符串长度，则默认为0\n",
    "            digit1 = int(num1[i]) if i<len(num1) else 0\n",
    "            digit2 = int(num2[i]) if i<len(num2) else 0\n",
    "            curr_sum = digit1+digit2+carry  # 计算当前位的和，加上进位\n",
    "            carry = curr_sum//10  # 更新进位\n",
    "            res.append(str(curr_sum%10))  # 加入当前值\n",
    "            i += 1\n",
    "        \n",
    "        # 处理最高位的进位\n",
    "        if carry > 0:\n",
    "            res.append(str(carry))\n",
    "        \n",
    "        # 反转结果字符串并返回\n",
    "        res = res[::-1]\n",
    "        return \"\".join(res)\n",
    "        # \"\".join() 是一个字符串方法，用于将一个可迭代对象中的元素连接成一个字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        if len(num2) > len(num1):\n",
    "            num1, num2 = num2, num1\n",
    "        min_length = len(num2)\n",
    "        num1 = list(map(int, num1))[::-1]\n",
    "        num2 = list(map(int, num2))[::-1]\n",
    "        for i in range(min_length):\n",
    "            num1[i] += num2[i]\n",
    "        for i in range(len(num1)):\n",
    "            if i == len(num1) - 1:\n",
    "                if num1[i] > 9:\n",
    "                    num1[i] -= 10\n",
    "                    num1.append(1)\n",
    "            else:\n",
    "                if num1[i] > 9:\n",
    "                    num1[i] -= 10\n",
    "                    num1[i+1] += 1\n",
    "        return ''.join(map(str, num1[::-1]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "      \n",
    "        l1, l2 = len(num1), len(num2)\n",
    "        i, j = l1-1, l2-1\n",
    "        res = []\n",
    "        c = 0\n",
    "        while i>=0 and j>=0:\n",
    "            r = (int(num1[i]) + int(num2[j]) + c) % 10\n",
    "            c = (int(num1[i]) + int(num2[j]) + c) // 10\n",
    "            res.append(str(r))\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "\n",
    "        while i>=0:\n",
    "            r = (int(num1[i]) + c) % 10 \n",
    "            c = (int(num1[i]) + c) // 10\n",
    "            res.append(str(r))\n",
    "            i -= 1 \n",
    "\n",
    "        while j>=0:\n",
    "            r = (int(num2[j]) + c) % 10 \n",
    "            c = (int(num2[j]) + c) // 10\n",
    "            res.append(str(r))\n",
    "            j -= 1\n",
    "\n",
    "        if c > 0:\n",
    "            res.append(str(c))\n",
    "            \n",
    "        res = res[::-1]\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i, j = len(num1) - 1, len(num2) - 1\n",
    "        ans = []\n",
    "        c = 0\n",
    "        while i >= 0 or j >= 0 or c:\n",
    "            a = 0 if i < 0 else int(num1[i])\n",
    "            b = 0 if j < 0 else int(num2[j])\n",
    "            c, v = divmod(a + b +c, 10)\n",
    "            ans.append(str(v))\n",
    "            i, j = i - 1, j - 1\n",
    "        return \"\".join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        max_len = max(len(num1), len(num2))\n",
    "        num1_lst = [int(_) for _ in num1][::-1]\n",
    "        num2_lst = [int(_) for _ in num2][::-1]\n",
    "\n",
    "        carry = 0\n",
    "        r_lst = []\n",
    "        for i in range(max_len):\n",
    "            v1 = num1_lst[i] if i < len(num1_lst) else 0\n",
    "            v2 = num2_lst[i] if i < len(num2_lst) else 0\n",
    "            s = v1 + v2 + carry\n",
    "            r_lst.append(s % 10)\n",
    "            carry = s // 10\n",
    "            if i == max_len - 1 and carry > 0:\n",
    "                r_lst.append(carry)\n",
    "\n",
    "        return ''.join([str(_) for _ in r_lst[::-1]])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        carry = 0\n",
    "        ans = []\n",
    "        for b1, b2 in zip_longest(reversed(num1), reversed(num2), fillvalue=\"0\"):\n",
    "            r = int(b1) + int(b2) + carry\n",
    "            carry = r >= 10\n",
    "            ans.append(str(r % 10))\n",
    "        \n",
    "        if carry:\n",
    "            ans.append(\"1\")\n",
    "            \n",
    "        return \"\".join(reversed(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i, j = len(num1) - 1, len(num2) - 1\n",
    "        ans = []\n",
    "        c = 0\n",
    "        while i >= 0 or j >= 0 or c:\n",
    "            a = 0 if i < 0 else int(num1[i])\n",
    "            b = 0 if j < 0 else int(num2[j])\n",
    "            c, v = divmod(a + b + c, 10)\n",
    "            ans.append(str(v))\n",
    "            i, j = i - 1, j - 1\n",
    "        return \"\".join(ans[::-1])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "\n",
    "        # 从最后开始\n",
    "        large = num1 if len(num1) > len(num2) else num2\n",
    "        small = num1 if len(num1) <= len(num2) else num2\n",
    "        large = large[::-1]\n",
    "        small = small[::-1]\n",
    "\n",
    "        carry = 0\n",
    "        output = []\n",
    "        for i in range(len(large)):\n",
    "            l = int(large[i])\n",
    "            s = int(small[i]) if i < len(small) else 0\n",
    "\n",
    "            sum = l + s +carry\n",
    "            output.append(str(sum % 10))\n",
    "            carry = sum // 10\n",
    "        \n",
    "        if carry:\n",
    "            output.append(str(carry))\n",
    "        return \"\".join(output)[::-1]\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addStrings(self, num1: str, num2: str) -> str:\n",
    "        i, j = 0, 0\n",
    "        n, m = len(num1), len(num2)\n",
    "\n",
    "        res = []\n",
    "        \n",
    "        k = 0\n",
    "        while i < n and j < m:\n",
    "            a, b = int(num1[n - i - 1]), int(num2[m - j - 1])\n",
    "            s = a + b + k\n",
    "            k, r = s // 10, s % 10\n",
    "            res.append(str(r))\n",
    "\n",
    "            i += 1\n",
    "            j += 1\n",
    "        \n",
    "        while i < n:\n",
    "            a = int(num1[n - i - 1])\n",
    "            s = a + k\n",
    "            k, r = s // 10, s % 10\n",
    "            res.append(str(r))\n",
    "\n",
    "            i += 1\n",
    "\n",
    "        while j < m:\n",
    "            b = int(num2[m - j - 1])\n",
    "            s = b + k\n",
    "            k, r = s // 10, s % 10\n",
    "            res.append(str(r))\n",
    "\n",
    "            j += 1\n",
    "        \n",
    "        if k:\n",
    "            res.append(str(k))\n",
    "\n",
    "        res.reverse()\n",
    "        return ''.join(res)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
